package com.kczd.adapter;



import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.kczd.client.R;
import com.kczd.entity.Location;

import java.util.List;


public class LocationAdapter extends BaseAdapter{

	private LayoutInflater mInflater;
	/**data*/
	private List<Location> locations;
	private int resource;
	/**对属性(实例变量)初始化*/

	public LocationAdapter(){}
	
	public LocationAdapter(Context context, 
			int resource,List<Location> locations) {
		mInflater =(LayoutInflater)
				context.getSystemService(
						Context.LAYOUT_INFLATER_SERVICE);
		this.resource=resource;
		this.locations=locations;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return locations.size();//数量
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return locations.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		Log.i("TAG", "convertView="+convertView);
		View view=null;
		ViewHolder vh=null;
		if(convertView==null){
			view=mInflater.inflate(resource,parent, false);
			vh=new ViewHolder();
			vh.typeIV=(ImageView) view.findViewById(R.id.iv_type);
            vh.datetimeTV=(TextView) view.findViewById(R.id.tv_date);
            vh.originTV=(TextView) view.findViewById(R.id.tv_origin);
            vh.terminalTV=(TextView) view.findViewById(R.id.tv_terminal);

            view.setTag(vh);

		}else{
			view=convertView;
			vh=(ViewHolder) view.getTag();
		}
		Location location=(Location) getItem(position);
		vh.typeIV.setImageResource(location.getType());
		vh.datetimeTV.setText(location.getDatetime());
		vh.originTV.setText(location.getOrigin());
		vh.terminalTV.setText(location.getTerminal());
		return view;

	}
	class ViewHolder{//类名任意，只要符合标识符规范
		ImageView typeIV;
		TextView  datetimeTV;
		TextView  originTV;
		TextView  terminalTV;
	}
}
